
import 'package:hrf_app/common/api/bug/bug.dart';
import 'package:hrf_app/common/models/bug/project_bug_model.dart';
import 'package:hrf_app/common/models/bug/project_user_model.dart';
import 'package:hrf_app/common/routers/names.dart';
import 'package:hrf_app/pages/menu/bug_page_list/projectBugPage/model/searchProjectBugModel.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class ProjectBugPageController extends GetxController {
  ProjectBugPageController();

  List<ProjectBugModel> projectBugList = <ProjectBugModel>[];
  SearchProjectBugModel searchProjectBugModel = SearchProjectBugModel();
  List<TDDropdownItemOption> statusWidgetList = [];
  List<TDDropdownItemOption> userWidgetList = [];
  List<TDDropdownItemOption> projectWidgetList = [];
  bool isLoading = false;
  late EasyRefreshController easyRefreshController;

  List<ProjectUserModel> projectUserList = [];

  _initData() {
    Map params = Get.arguments;
    String status = params["status"];
    List<String> statusInList = [];
    statusInList.add(status);
    searchProjectBugModel.statusInList = statusInList;
    getProjectBugListPage(searchProjectBugModel);
    getPjProjectUserList();
  }

  getPjProjectUserList() async {
    List<ProjectUserModel> userList = [];
    Map<String, dynamic> res = await BugApi.getPjProjectUserListApi({});
    for (var item in res["data"]) {
      userList.add(ProjectUserModel.fromJson(item));
    }
    projectUserList = userList;
  }

  getProjectBugListPage(params) {
    BugApi.getPjProjectBugPageListApi(params).then((res) {
      if (res["code"] == "00000") {
        projectBugList = [];
        for (var item in res["data"]["records"]) {
          projectBugList.add(ProjectBugModel.fromJson(item));
        }
        update(["projectBugPage"]);
      }
    });
  }

  onJumpToDetail(id) {
    Get.toNamed(RouteNames.projectBugDetailPage, arguments: id);
  }

  @override
  void onInit() {
    easyRefreshController = EasyRefreshController(
      controlFinishRefresh: true,
      controlFinishLoad: true,
    );
    userWidgetInit();

    projectWidgetInit();
    super.onInit();
  }

  void editAssignUserDialog(
      String content, String filedName, String title, String id) async {
    Picker picket = Picker(
        adapter: PickerDataAdapter<String>(
            pickerdata: projectUserList.map((item) => item.realName).toList()),
        hideHeader: true);
    BuildContext? buildContext = Get.context!;
    Navigator.of(buildContext).push(TDSlidePopupRoute(
        slideTransitionFrom: SlideTransitionFrom.bottom,
        builder: (buildContext) {
          return TDPopupBottomConfirmPanel(
              title: "指派人 ",
              leftClick: () {
                Navigator.maybePop(buildContext);
              },
              rightClick: () {
                List<dynamic> value = picket.getSelectedValues();
                List<int> index = picket.selecteds;
                if (value.isEmpty) return;
                ProjectUserModel projectUserModel = projectUserList[index[0]];
                String userId = projectUserModel.userId!;
                updateUser(userId, id);
                Navigator.maybePop(buildContext);
              },
              child: picket.makePicker());
        }));
  }

  //修改指派人
  updateUser(String userId, String id) async {
    Map map = Map();
    map['assignUserId'] = userId;
    map['id'] = id;
    Map<String, dynamic> res =
        await BugApi.updatePjProjectBugAssignUserIdApi(map);
    if (res["code"] == "00000") {
      TDToast.showText('修改成功', context: Get.context!);
      getProjectBugListPage(searchProjectBugModel);
    }
  }

  void projectWidgetInit() {
    BugApi.getPjProjectByUserIdApi({"id": "1111"}).then((res) {
      if (res["code"] == "00000") {
        for (var item in res["data"]) {
          projectWidgetList.add(
              TDDropdownItemOption(value: item["id"], label: item["name"]));
        }
        update(["projectBugPage"]);
      }
    });
  }

  void userWidgetInit() {
    BugApi.getPjProjectUserListApi({"projectId": "1"}).then((res) {
      if (res["code"] == "00000") {
        for (var item in res["data"]) {
          userWidgetList.add(TDDropdownItemOption(
              value: item["userId"], label: item["realName"]));
        }
        update(["projectBugPage"]);
      }
    });
  }

  statusWidgetInit() {
    TDDropdownItemOption tdDropdownItemOption0 =
        TDDropdownItemOption(value: "0", label: "未完成");
    TDDropdownItemOption tdDropdownItemOption2 =
        TDDropdownItemOption(value: "5", label: "已暂停");
    TDDropdownItemOption tdDropdownItemOption3 =
        TDDropdownItemOption(value: "10", label: "已解决");
    TDDropdownItemOption tdDropdownItemOption4 =
        TDDropdownItemOption(value: "20", label: "已关闭");
    statusWidgetList.add(tdDropdownItemOption0);
    statusWidgetList.add(tdDropdownItemOption2);
    statusWidgetList.add(tdDropdownItemOption3);
    statusWidgetList.add(tdDropdownItemOption4);
  }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  void onRefresh() {
    searchProjectBugModel.pageNum = 1;
    BugApi.getPjProjectBugPageListApi(searchProjectBugModel).then((res) {
      if (res["code"] == "00000") {
        projectBugList = [];
        for (var item in res["data"]["records"]) {
          projectBugList.add(ProjectBugModel.fromJson(item));
        }
        easyRefreshController.finishRefresh(IndicatorResult.success);
        easyRefreshController.resetFooter();
        update(["projectBugPage"]);
      }
    });
  }

  void onLoad() {
    searchProjectBugModel.pageNum = searchProjectBugModel.pageNum + 1;
    BugApi.getPjProjectBugPageListApi(searchProjectBugModel).then((res) {
      if (res["code"] == "00000") {
        List<dynamic> projectBugResList = [];
        projectBugResList = res["data"]["records"];
        if (projectBugResList.isEmpty) {
          easyRefreshController.finishLoad(IndicatorResult.noMore);
          searchProjectBugModel.pageNum--;
        }
        if (projectBugResList.isNotEmpty) {
          for (var item in projectBugResList) {
            projectBugList.add(ProjectBugModel.fromJson(item));
          }
          easyRefreshController.finishLoad();
        }
        update(["projectBugPage"]);
      }
    });
  }

  void statusOnChange(val) {
    easyRefreshController.resetFooter();
    searchProjectBugModel.pageNum = 1;
    searchProjectBugModel.statusInList = val;
    getProjectBugListPage(searchProjectBugModel);
  }

  void statusOnReset() {
    searchProjectBugModel.pageNum = 1;
    searchProjectBugModel.statusInList = [];
    getProjectBugListPage(searchProjectBugModel);
  }

  void userOnChange(val) {
    easyRefreshController.resetFooter();
    searchProjectBugModel.pageNum = 1;
    searchProjectBugModel.userIdInList = val;
    projectBugList = [];
    getProjectBugListPage(searchProjectBugModel);
  }

  void userOnReset() {
    searchProjectBugModel.pageNum = 1;
    searchProjectBugModel.userIdInList = [];
    getProjectBugListPage(searchProjectBugModel);
  }

  void projectOnChange(val) {
    easyRefreshController.resetFooter();
    searchProjectBugModel.pageNum = 1;
    searchProjectBugModel.projectIdInList = val;
    projectBugList = [];
    getProjectBugListPage(searchProjectBugModel);
  }

  void projectOnReset() {
    searchProjectBugModel.pageNum = 1;
    searchProjectBugModel.projectIdInList = [];
    getProjectBugListPage(searchProjectBugModel);
  }

  void jumpToProjectButAdd() {
    Get.toNamed(RouteNames.projectBugAddPage);
  }

// @override
// void onClose() {
//   super.onClose();
// }
}
